package khleuven.mondzorg.domain;

import khleuven.mondzorg.Logger;

import org.json.JSONObject;

import android.util.Base64;

public class Illustration implements MondAppEntry{
	// TODO als de blob null is > uitlezen van het www updateMethode()
	
	
	private String filename;
	private String altTekst;
	private String link = "";
	private byte[] blob;
	 
	@Deprecated
	public Illustration(String filename, String altTekst, String link) throws DomainException{
		setFileName(filename);
		setAltTekst(altTekst);
		setLink(link);
		registerImage(BlobLib.getIllustrationBlob(link));
	}
	
	@Deprecated
	public Illustration(String filename, String altTekst, byte[] blob){
		setFileName(filename);
		setAltTekst(altTekst);
		registerImage(blob);
	}
	
	public Illustration(JSONObject object) throws DomainException{
		setFileName(JsonKeysDictionary.getStringItem(object, JsonKeysDictionary.JSON_KEY_ILL_NAME));
		setAltTekst(JsonKeysDictionary.getStringItem(object, JsonKeysDictionary.JSON_KEY_ILL_DESCRIPTION));
		setBlob(Base64.decode(JsonKeysDictionary.getStringItem(object, JsonKeysDictionary.JSON_KEY_ILL_BLOB), Base64.DEFAULT));
	}
	
	public Illustration(JSONObject object, byte[] blob) throws DomainException{
		setFileName(JsonKeysDictionary.getStringItem(object, JsonKeysDictionary.JSON_KEY_ILL_NAME));
		setAltTekst(JsonKeysDictionary.getStringItem(object, JsonKeysDictionary.JSON_KEY_ILL_DESCRIPTION));
		registerImage(blob);
		Logger.logDomain(toString());
	}
	
	public void registerImage(byte[] blob) {
		// can be empty if no image is found...
		if(blob == null) {
			setBlob(new byte[0]);
		}
		setBlob(blob);
	}

	private void setFileName(String filename) {
		this.filename = filename;
	}

	//TODO: als leeg - show description!
	public byte[] getBlob() {
		return blob;
	}

	private void setBlob(byte[] blob) {
		this.blob = blob;
	}

	public String getAltTekst() {
		return altTekst;
	}

	private void setAltTekst(String altTekst) {
		this.altTekst = altTekst;
	}
	
	@Override
	public String toString(){
		return super.toString()+getAltTekst();				
	}
	
	@Override
	public boolean equals(Object o){
		boolean gelijk = false;
		if(o instanceof Illustration){
			Illustration i = (Illustration)(o);	
			gelijk = this.getFileName().equals(i.getFileName());
		}
		return gelijk;
	}
	
	public String getFileName() {
		return this.filename;
	}

	public String getLink() {
		return link;
	}

	public void setLink(String link) {
		this.link = link;
	}
	
	public JSONObject toJSon() throws DomainException{
		JSONObject output = new JSONObject();
		JsonKeysDictionary.toStringItem(output, JsonKeysDictionary.JSON_KEY_ILL_NAME, getFileName());
		JsonKeysDictionary.toStringItem(output, JsonKeysDictionary.JSON_KEY_ILL_DESCRIPTION, getAltTekst());
		String encodedIll = Base64.encodeToString(getBlob(), Base64.DEFAULT);		
		JsonKeysDictionary.toStringItem(output, JsonKeysDictionary.JSON_KEY_ILL_BLOB, encodedIll);
		return output;
	}
}
